<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      :inline="true"
      label-width="90px"
    >
      <el-form-item label="任务名称" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务类型" prop="taskType">
        <el-input
          v-model="queryParams.taskType"
          placeholder="请输入任务类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据库名称" prop="databaseName">
        <el-input
          v-model="queryParams.databaseName"
          placeholder="请输入数据库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
        >重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['console:tasks:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['console:tasks:edit']"
            >修改</el-button
          >
        </el-col> -->
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['console:tasks:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['console:tasks:export']"
            >导出</el-button
          >
        </el-col> -->
      <right-toolbar
        :show-search.sync="showSearch"
        @queryTable="getList"
      />
    </el-row>

    <el-table
      v-loading="loading"
      :data="tasksList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="编号" width="50" />
      <el-table-column label="任务名称" align="center" prop="taskName" />
      <el-table-column label="任务类型" align="center" prop="taskType" />
      <el-table-column label="数据库名称" align="center" prop="databaseName" />
      <el-table-column label="任务状态" align="center" prop="taskStatus">
        <template slot-scope="scope">
          <div v-for="dict in dict.type.taskstatus" :key="dict.value">
            {{ scope.row.taskStatus == dict.value ? dict.label : "" }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['console:tasks:result']"
            size="mini"
            type="text"
            icon="el-icon-document-checked"
            @click="handleResult(scope.row)"
          >评估报告</el-button>
          <el-button
            v-hasPermi="['console:tasks:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['console:tasks:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.startNum"
      :limit.sync="queryParams.pgSize"
      @pagination="getList"
    />

    <!-- 添加或修改评估任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务名称" prop="taskName">
          <el-input v-model="form.taskName" placeholder="请输入任务名称" />
        </el-form-item>
        <el-form-item label="任务类型" prop="taskType">
          <el-input v-model="form.taskType" placeholder="请输入任务类型" />
        </el-form-item>
        <el-form-item label="数据源ID" prop="id">
          <el-input v-model="form.id" placeholder="请输入数据源ID" />
        </el-form-item>
        <el-form-item label="数据库名称" prop="databaseName">
          <el-input
            v-model="form.databaseName"
            placeholder="请输入数据库名称"
          />
        </el-form-item>
        <el-form-item label="对象类型" prop="taskWay">
          <el-input v-model="form.taskWay" placeholder="请输入对象类型" />
        </el-form-item>
        <el-form-item label="统计信息" prop="taskCount">
          <el-input v-model="form.taskCount" placeholder="请输入统计信息" />
        </el-form-item>
        <el-form-item label="统计模块" prop="taskMode">
          <el-input v-model="form.taskMode" placeholder="请输入统计模块" />
        </el-form-item>
        <el-form-item label="执行策略" prop="strategy">
          <el-input v-model="form.strategy" placeholder="请输入执行策略" />
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input v-model="form.desc" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="描述" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTasks,
  getTasks,
  delTasks,
  addTasks,
  updateTasks
} from '@/api/console/tasks'

export default {
  name: 'Tasks',
  dicts: ['taskstatus'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 评估任务表格数据
      tasksList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        startNum: 1,
        pgSize: 10,
        taskName: null,
        taskType: null,
        databaseName: null,
        taskStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询评估任务列表 */
    getList() {
      this.loading = true
      listTasks(this.queryParams).then((response) => {
        this.tasksList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        taskName: null,
        taskType: null,
        id: null,
        databaseName: null,
        taskWay: null,
        taskCount: null,
        taskMode: null,
        taskTables: null,
        taskStatus: null,
        strategy: null,
        desc: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.startNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({ path: '/synchronous/synchronousAdd' })
      this.title = '添加评估任务'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.title = '修改评估任务'
      this.$router.push({
        path: '/steps/stepsIndex',
        query: { id: row.id || this.ids }
      })
    },
    // 评估报告按钮
    handleResult(row) {
      this.$router.push({ path: '/reports/index', query: { id: row.id }})
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateTasks(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addTasks(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let ids = row.id || this.ids
      if (Object.prototype.toString.call(ids) !== '[object Array]') {
        ids = [ids]
      }
      this.$modal
        .confirm('是否确认删除评估任务编号为"' + ids + '"的数据项？')
        .then(function() {
          return delTasks({ ids })
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    }
  }
}
</script>
